package com.wl.wuliu.controller;

import com.wl.wuliu.core.controller.BaseController;
import com.wl.wuliu.core.page.TableDataInfo;
import com.wl.wuliu.entity.PasswordBody;
import com.wl.wuliu.entity.SysUser;
import com.wl.wuliu.entity.Truck;
import com.wl.wuliu.result.AjaxResult;
import com.wl.wuliu.service.SysUserService;
import com.wl.wuliu.utils.PatternUtils;
import com.wl.wuliu.utils.StringUtils;
import com.wl.wuliu.weixin.AccessToken;
import com.wl.wuliu.weixin.Token;
import com.wl.wuliu.weixin.WeixinHttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author: gutaozhen
 * @Date: 2024/5/15
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表查询
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo findList(SysUser sysUser) {

        startPage();
        List<SysUser> list = sysUserService.findList(sysUser);

        return getDataTable(list);
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody SysUser sysUser) {

        if (StringUtils.isEmpty(sysUser.getNickName())) {
            return AjaxResult.error("联络员姓名不能为空。");
        }

        if (StringUtils.isEmpty(sysUser.getMobile())) {
            return AjaxResult.error("手机号不能为空。");
        }else {
            if (!PatternUtils.validatePhoneNumber(sysUser.getMobile())) {
                return AjaxResult.error("手机号不规范。");
            }
        }

        return sysUserService.saveSysUser(sysUser);
    }

    /**
     * 修改密码
     * @param passwordBody
     * @return
     */
    @PostMapping("/updatePwd")
    public AjaxResult updatePwd(@RequestBody PasswordBody passwordBody) {

        if (StringUtils.isEmpty(passwordBody.getUserId())) {
            return AjaxResult.error("参数错误。");
        }

        if (StringUtils.isEmpty(passwordBody.getOldPassword())) {
            return AjaxResult.error("原始密码不能为空。");
        }

        if (StringUtils.isEmpty(passwordBody.getNewPassword())) {
            return AjaxResult.error("新密码不能为空。");
        }

        return sysUserService.updatePwd(passwordBody);
    }

    /**
     * 登录时获取微信用户的详细信息
     * @return
     */
    @GetMapping("/getWeixinUserInfo")
    public AjaxResult getWeixinUserInfo(@RequestParam String code) {

        return AjaxResult.success(sysUserService.getWeixinUserInfo(code));
    }

}
